package com.aion.jirc.irc;

import java.io.BufferedWriter;

/**
 * Created by IntelliJ IDEA.
 * Date: 11-02-16
 * Time: 21:00
 *
 * @author Aion
 */
public class OutputHandler {

    private final BufferedWriter bw;

    /**
     * Initializes the output handler.
     *
     * @param bw
     *          the <code>BufferedWriter</code> instance
     * @throws Exception
     *          caught exceptions
     */
    protected OutputHandler(BufferedWriter bw) throws Exception {
        if (bw == null) {
            throw new Exception("Buffered writer is null");
        }
        this.bw = bw;
    }

    /**
     * Sends a <code>String</code> to the server and appends a carriage return.
     *
     * @param output
     *          the <code>String</code> to send
     */
    public final void send(String output) {
        if (output != null && !output.isEmpty()) {
            sendRaw(output + "\r\n");
        }
    }

    /**
     * Sends a <code>String</code> to the server and does not append a carriage return.
     *
     * @param output
     *          the <code>String</code> to send
     */
    public final void sendRaw(String output) {
        if (output != null && !output.isEmpty()) {
            try {
                bw.write(output);
                bw.flush();
            } catch (Exception e) {
            System.out.println("ERROR: " + e); }
        }
    }

}
